//
//  CustomerEditRemarkVC.m
//  New_Runway
//
//  Created by mack on 2018/2/11.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CustomerEditRemarkVC.h"

@interface CustomerEditRemarkVC ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UILabel *numLabel;
@property (nonatomic, copy) NSString *remark;

@end

@implementation CustomerEditRemarkVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarView.titleLabel.text = @"修改备注";
    self.view.backgroundColor = kColorWhite();
    self.textView.delegate = self;
    self.textView.tintColor = MAIN_NAV_COLOR();
    [self.navigationBarView.rightItem setTitle:@"保存" forState:UIControlStateNormal];
    if (self.reStr) {
        self.textView.text = self.reStr;
    }else{
        self.textView.text = @"请输入备注内容";
        _remark = @"";
    }

}
-(void)textViewDidChange:(UITextView *)textView{
    NSLog(@"%lu",(unsigned long)textView.text.length);
    
    if ([textView.text length] > 50) {
        textView.text = [textView.text substringWithRange:NSMakeRange(0, 50)];
        [textView.undoManager removeAllActions];
        [textView becomeFirstResponder];
        return;
    }
    
     _remark = textView.text;
    
    self.numLabel.text = [NSString stringWithFormat:@"(%lu/50)",(unsigned long)textView.text.length];
    
}

-(void)textViewDidBeginEditing:(UITextView *)textView{
    
    if([textView.text isEqualToString:@"请输入备注内容"]){
        textView.text = @"";
        _remark = @"";
    }
    
}
-(void)rightItemClick{
    NSString *url = [NSString stringWithFormat:@"%@%@",CRMContextApi,customerEditRemarkApi];
    if ([_remark isEqualToString:@""]) {
        [ShowMessage showHintMessage:@"请输入备注内容!" duration:2.0 withRemindId:remindWarning];

        return;
    }
    
    [YQNetworking postWithUrl:url refreshRequest:YES cache:NO params:@{@"customerShopId":self.customerId,@"customerRemark":_remark} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response) {
        if ([response[@"code"] isEqual:@1]) {
            [self.navigationController popViewControllerAnimated:YES];
            self.block(_remark);
        }
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];

    }];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
